<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- 引入JQuery -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/js/bootstrap.min.js"></script>
<!-- DataTables JS -->
<script type="text/javascript" src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.23/js/dataTables.bootstrap4.min.js"></script>
<script type="text/javascript">
    var ctx = '${pageContext.request.contextPath}';
</script>

<!-- 菜单变色 -->
<script type="text/javascript">
    $(function () {
        var url = location.href;
        if (url.indexOf('products.jsp') > -1) {
            $('#productMenu').addClass('active');
        } else if (url.indexOf('profile') > -1) {
            $('#profileMenu').addClass('active');
        }
    });
</script>

<c:if test="${!empty userSession}">
    <script type="text/javascript">
        var websocket;
        if (typeof (WebSocket) == "undefined") {
            console.log("您的浏览器不支持WebSocket");
        } else {
            console.log("您的浏览器支持WebSocket");
            //实现化WebSocket对象，指定要连接的服务器地址与端口  建立连接
            //等同于
            websocket = new WebSocket("ws://localhost:8080/message");
            //socket = new WebSocket("${ctx}websocket/${cid}".replace("http","ws"));

            //打开事件
            websocket.onopen = function () {
                console.log("Socket 已打开");
                // websocket.send("这是来自客户端的消息" + location.href + new Date());
            };

            //获得消息事件
            websocket.onmessage = function (msg) {
                console.log(msg.data);
                //发现消息进入    开始处理前端触发逻辑
                $('#liveToast .toast-body').html(msg.data);
                $('#liveToast').toast('show');
            };

            //关闭事件
            websocket.onclose = function () {
                console.log("Socket已关闭");
            };

            //发生了错误事件
            websocket.onerror = function () {
                alert("Socket发生了错误");
                //此时可以尝试刷新页面
            }

        }
    </script>
</c:if>